<template>
  <!-- 班级辅导员页面 -->
  <div class="app-container">
    <searchForm
      :formOptions="formOptions"
      @onSearch="onSearch"
      @onReset="onReset"
      :formLabelWidth="formLabelWidth"
      v-show="showSearch"
    />
    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table :data="roleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column label="年级" prop="gradeName" align="center" width="55"/>
      <el-table-column
        label="院系"
        prop="deptNames"
        :show-overflow-tooltip="true"
        align="center"
      />
      <el-table-column label="专业" prop="specialtyName" align="center" />
      <el-table-column label="班号" prop="classCode" align="center"  width="100"/>
      <el-table-column label="班级名称" prop="name" align="center" />
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            >任命辅导员</el-button
          >
          <el-button size="mini" type="text" @click="ShiftViewing(scope.row)"
            >取消任命</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 任命辅导员弹框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <h3>班级信息</h3>
      <el-form
        :model="divisionInfor"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item label="年级">
          <el-input v-model="divisionInfor.gradeName" disabled />
        </el-form-item>

        <el-form-item label="院系">
          <el-input v-model="divisionInfor.deptNames" disabled />
        </el-form-item>

        <el-form-item label="专业">
          <el-input v-model="divisionInfor.specialtyName" disabled />
        </el-form-item>

        <el-form-item label="班级">
          <el-input v-model="divisionInfor.name" disabled />
        </el-form-item>
      </el-form>
      <h3>查询条件</h3>
      <el-form
        :model="counselorParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item label="教师工号" prop="tno">
          <el-input
            v-model="counselorParams.tno"
            placeholder="请输入教师工号"
            clearable
            style="width: 200px"
            @keyup.enter.native="clrQuery"
          />
        </el-form-item>
        <el-form-item label="教师姓名" prop="thrName">
          <el-input
            v-model="counselorParams.thrName"
            placeholder="请输入教师姓名"
            clearable
            style="width: 200px"
            @keyup.enter.native="clrQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="clrQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-table :data="counselorList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="教师工号" prop="tno" align="center" />
        <el-table-column
          label="教师姓名"
          prop="thrName"
          :show-overflow-tooltip="true"
          align="center"
        />
        <el-table-column label="性别" prop="sex" align="center">
        <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
          </template>
        </el-table-column>
        <el-table-column label="联系电话" prop="teacher.phone" align="center" />
      </el-table>
      <pagination
        v-show="counselorTotal > 0"
        :total="counselorTotal"
        :page.sync="counselorParams.pageNum"
        :limit.sync="counselorParams.pageSize"
        @pagination="getClr"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 取消任命 -->
    <el-dialog
      :title="title"
      :visible.sync="appointOpen"
      width="60%"
      append-to-body
    >
      <el-form
        :model="ctrParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item label="教师工号" prop="roleName">
          <el-input
            v-model="ctrParams.roleKey"
            placeholder="请输入教师工号"
            clearable
            style="width: 200px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="教师姓名" prop="roleKey">
          <el-input
            v-model="ctrParams.roleKey"
            placeholder="请输入教师姓名"
            clearable
            style="width: 200px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-table :data="classCtrList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="教师工号" prop="roleId" align="center" />
        <el-table-column
          label="教师姓名"
          prop="roleName"
          :show-overflow-tooltip="true"
          align="center"
        />
        <el-table-column label="性别" prop="roleKey" align="center" />
        <el-table-column label="联系方式" prop="roleKey" align="center" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="ctrTotal > 0"
        :total="ctrTotal"
        :page.sync="ctrParams.pageNum"
        :limit.sync="ctrParams.pageSize"
        @pagination="getList"
      />
    </el-dialog>
  </div>
</template>

<script>
import {
  listRole,
  getRole,
  delRole,
  addRole,
  updateRole,
  dataScope,
  changeRoleStatus,
  deptTreeSelect,
} from "@/api/system/role";
import { clrClassList,classClrOrCsThr} from "@/api/counselor/division";
import { classList } from "@/api/information/classInfor";
import searchForm from "@/components/Search/searchForm.vue";

export default {
  name: "Role",
  dicts: [
    "sys_normal_disable",
    "sys_user_sex"
],
  components: { searchForm },
  data() {
    return {
      formLabelWidth: "70px",
      formOptions: [
        {
          label: "年级",
          prop: "gradeName",
          element: "el-input",
          placeholder: "请输入年级",
        },
        // {
        //   label: "院系",
        //   prop: "YX",
        //   element: "el-input",
        //   placeholder: "请输入院系",
        // },
        {
          label: "专业",
          prop: "specialtyName",
          element: "el-input",
          placeholder: "请输入专业",
        },
        {
          label: "班级名称",
          prop: "name",
          element: "el-input",
          placeholder: "请输入名称",
        }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      counselorTotal: 0,
      ctrTotal: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 取消任命弹框
      appointOpen: false,
      // 是否显示弹出层（数据权限）
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      divisionInfor: {},
      counselorList: [], // 辅导员列表
      classCtrList: [], // 班级下的辅导员列表
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleName: undefined,
        roleKey: undefined,
        status: undefined,
      },
      counselorParams: {
        pageNum: 1,
        pageSize: 10,
        type:1
      },
      // 分班查看
      ctrParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        roleName: [
          { required: true, message: "角色名称不能为空", trigger: "blur" },
        ],
        roleKey: [
          { required: true, message: "权限字符不能为空", trigger: "blur" },
        ],
        roleSort: [
          { required: true, message: "角色顺序不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取搜索表单提交的数据
    onSearch(val) {
      // 查询条件和分页数据合并
      this.queryParams = Object.assign(this.queryParams, val);
      this.queryParams.pageNum = 1;
      this.getList();
    },
     // 重置
     onReset(data) {
      this.queryParams.pageNum = 1;
      this.queryParams = Object.assign(this.queryParams, data);
      this.getList();
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      classList(this.queryParams).then(
        (response) => {
          this.roleList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 角色状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal
        .confirm('确认要"' + text + '""' + row.roleName + '"角色吗？')
        .then(function () {
          return changeRoleStatus(row.roleId, row.status);
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.status = row.status === "0" ? "1" : "0";
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    // 分班查看弹框
    ShiftViewing(row) {
      this.title = "取消任命";
      this.appointOpen = true;
      classClrOrCsThr(row.id,1).then(res => {
        console.log(res)
        this.classCtrList = res.rows
        this.ctrTotal = res.total
      })
    },

   
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.counselorParams = {}
      this.counselorParams.type = 1
      this.getClr()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.roleId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    
    /** 班级分配辅导员操作 */
    handleUpdate(row) {
      console.log(row)
      this.open = true;
      this.title = "班级分配辅导员";
      this.divisionInfor = row;
      this.getClr()
    },
    clrQuery() {
      this.getClr()
    },
    // 获取辅导员信息列表
    async getClr() {
      let res = await clrClassList(this.counselorParams)
      this.counselorList = res.rows
      this.counselorTotal = res.total;

    },
    /** 选择角色权限范围触发 */
    dataScopeSelectChange(value) {
      if (value !== "2") {
        this.$refs.dept.setCheckedKeys([]);
      }
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.roleId != undefined) {
            this.form.menuIds = this.getMenuAllCheckedKeys();
            updateRole(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.menuIds = this.getMenuAllCheckedKeys();
            addRole(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮（数据权限） */
    submitDataScope: function () {
      if (this.form.roleId != undefined) {
        this.form.deptIds = this.getDeptAllCheckedKeys();
        dataScope(this.form).then((response) => {
          this.$modal.msgSuccess("修改成功");
          this.openDataScope = false;
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const roleIds = row.roleId || this.ids;
      this.$modal
        .confirm('是否确认删除角色编号为"' + roleIds + '"的数据项？')
        .then(function () {
          return delRole(roleIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>